﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Monitor.Core.Sensor
{
    abstract class AbstractSensor : ISensor
    {
        protected int _id;
        protected string _description;
        protected DateTime _lastRefreshTime;
        protected int _interval;
        protected string _lastError;
        protected ServiceStatus _status;
        protected Uri _uri;

        public AbstractSensor(int id, Uri uri)
        {
            _id = id;
            _uri = uri;
        }

        public int Id
        {
            get { return _id; }
        }

        public Uri Uri
        {
            get { return _uri; }
        }

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        protected abstract ServiceStatus PingService();

        public ServiceStatus Ping()
        {
            _lastRefreshTime = DateTime.Now;
            try
            {
                this._lastError = string.Empty;
                _status = PingService();
                return _status;
            }
            catch (Exception ex)
            {
                this._lastError = ex.Message;
                _status = ServiceStatus.Dead;
                return _status;
            }
        }

        public DateTime LastRefreshTime
        {
            get { return _lastRefreshTime; }
        }

        public int Interval
        {
            get { return _interval; }
            set { _interval = value; }
        }

        public string LastError
        {
            get { return _lastError; }
        }

        public ServiceStatus Status
        {
            get { return _status; }
        }
    }
}
